﻿using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using CE.User;
using CE.Global;
using CE.Database;
using CE.UserAccess;

public partial class admin_agent_target_amount : System.Web.UI.Page
{
    string PageCode = "GGRPTRADBRATA";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/ManagementSecureLogin.aspx");
        }
        if (!IsPostBack)
        {
            ClearProcess();
            int _cryr = DateTime.Now.Year;
            int _styr = _cryr;
            ddlTargetYear.Items.Clear();
            for (int _year = _cryr; _year >= _styr; _year--)
            {
                ddlTargetYear.Items.Add(new ListItem(_year.ToString(), _year.ToString()));
            }

            ddlUserRole.Enabled = true;
            if (Convert.ToInt64(Session[General.UserId].ToString()) != 1)
            {
                CheckPageAccess();
                ddlUserRole.Text = "100";
                ddlUserRole.Enabled = false;
            }
            txtActiveStatus.Text = "1";
            txtAgentId.Text = "-1";
            if (Request.QueryString["BrokerId"] != null)
            {
                Int64 AgentId = 0;
                if (!Int64.TryParse(Request.QueryString["BrokerId"].ToString(), out AgentId))
                {
                    AgentId = 0;
                }
                txtAgentId.Text = AgentId.ToString();
            }
            if (txtAgentId.Text.Trim() == "-1")
            {
                LoadBrokerList();
            }
            else
            {
                LoadBrokerListTarget();
            }
            if (txtActiveStatus.Text.Trim() == "0")
            {
                btnSubmit.Enabled = false;
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            if (Session[CE.Global.General.UserId] == null)
            {
                Response.Redirect("~/ManagementSecureLogin.aspx");
            }
            lblMessage.Text = "";
            lblError.Text = "";
            Int64 _result = -1;
            Int32 TargetYear = Convert.ToInt32(ddlTargetYear.Text);
            Int64 AgentId = Convert.ToInt32(ddlUser.Text);
            Int16 IsRenewal = Convert.ToInt16(ddlRenewal.Text);
            Int16 Status = Convert.ToInt16(ddlStatus.Text);
            Int64 UserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
            if (AgentId == 0)
            {
                IsRenewal = 2;
            }
            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            SortedList _sPara = new SortedList();
            _sPara.Add("@TargetYear", TargetYear);
            _sPara.Add("@AgentId", AgentId);
            _sPara.Add("@IsRenewal", IsRenewal);
            _sPara.Add("@Jan", txtJanuary.Text.Trim());
            _sPara.Add("@Feb", txtFebruary.Text.Trim());
            _sPara.Add("@Mar", txtMarch.Text.Trim());
            _sPara.Add("@Apr", txtApril.Text.Trim());
            _sPara.Add("@May", txtMay.Text.Trim());
            _sPara.Add("@Jun", txtJune.Text.Trim());
            _sPara.Add("@Jul", txtJuly.Text.Trim());
            _sPara.Add("@Aug", txtAugust.Text.Trim());
            _sPara.Add("@Sep", txtSeptember.Text.Trim());
            _sPara.Add("@Oct", txtOctober.Text.Trim());
            _sPara.Add("@Nov", txtNovember.Text.Trim());
            _sPara.Add("@Dec", txtDecember.Text.Trim());
            _sPara.Add("@UserId", UserId);
            _sPara.Add("@Status", Status);

            _result = _db.ExecuteSP("sprocAgentTargetAmountInsertUpdateSingleItem", _sPara);
            if (_result > 0)
            {
                ClearProcess();
                lblMessage.Text = "Successfully Created!";
                lblError.Text = "";
            }
            else
            {
                lblMessage.Text = "";
                lblError.Text = "Insert or Update Failed";
            }

        }
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        ClearProcess();
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        Response.Redirect("agent-target-amount.aspx");
    }

    protected void btnReLoad_Click(object sender, EventArgs e)
    {
        Int64 AgentId = -1;
        if (Request.QueryString["BrokerId"] != null)
        {
            if (!Int64.TryParse(Request.QueryString["BrokerId"].ToString(), out AgentId))
            {
                AgentId = -1;
            }
        }
        if (AgentId > -1)
        {
            Response.Redirect("agent-target-amount.aspx?BrokerId=" + AgentId.ToString());
        }
        else
        {
            Response.Redirect("agent-target-amount.aspx");
        }
    }

    protected void btnView_Click(object sender, EventArgs e)
    {
        Response.Redirect("view-agent-target-amount.aspx");
    }

    protected void ddlUserRole_SelectedIndexChanged(object sender, EventArgs e)
    {
        //ClearProcess();
        LoadBrokerList();
        trIsRenewal.Visible = false;
        if (ddlUserRole.Text == "100")
        {
            trIsRenewal.Visible = true;
        }
    }

    protected void ddlRenewal_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlUserRole.Text == "100" && ddlUserRole.SelectedIndex > 0)
        {
            LoadBrokerList();
        }
    }

    protected void ddlUser_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadBrokerTarget();
    }

    private void ClearProcess()
    {
        lblError.Text = "";
        lblMessage.Text = "";
        ddlRenewal.Text = "0";
        ddlStatus.Text = "";
        ddlUserRole.SelectedIndex = 0;
        ddlUser.SelectedIndex = 0;
        ClearFields();
        //txtJanuary.Text = "";
        //txtFebruary.Text = "";
        //txtMarch.Text = "";
        //txtApril.Text = "";
        //txtMay.Text = "";
        //txtJune.Text = "";
        //txtJuly.Text = "";
        //txtAugust.Text = "";
        //txtSeptember.Text = "";
        //txtOctober.Text = "";
        //txtNovember.Text = "";
        //txtDecember.Text = "";
    }

    private void ClearFields()
    {
        txtJanuary.Text = "";
        txtFebruary.Text = "";
        txtMarch.Text = "";
        txtApril.Text = "";
        txtMay.Text = "";
        txtJune.Text = "";
        txtJuly.Text = "";
        txtAugust.Text = "";
        txtSeptember.Text = "";
        txtOctober.Text = "";
        txtNovember.Text = "";
        txtDecember.Text = "";
    }

    private void LoadBrokerList()
    {
        ddlUser.Items.Clear();
        Int32 TargetYear = Convert.ToInt32(ddlTargetYear.Text);
        if (ddlUserRole.Text == "200")
        {
            ddlUser.Items.Clear();
            ddlUser.Items.Insert(0, new ListItem("Admin", "0"));
            ddlUser.Items.Insert(0, new ListItem("Select", "-1"));
        }
        else if (ddlUserRole.Text == "100")
        {
            Int16 User = 1;
            Int16 Renewal = Convert.ToInt16(ddlRenewal.Text);
            if (ddlUserRole.Text == "200")
            {
                User = 0;
                Renewal = 2;
            }
            MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
            DataSet _dsUserList = new DataSet();
            DataTable _dtUser = new DataTable();
            SortedList _sPara = new SortedList();
            _sPara.Add("@Step", 1);
            _sPara.Add("@User", User);
            _sPara.Add("@Renewal", Renewal);
            _sPara.Add("@TargetYear", TargetYear);

            _dsUserList = _db.GetDataSet("sprocTargetAmountAgentList", _sPara);
            if (_dsUserList != null && _dsUserList.Tables.Count > 0)
            {
                _dtUser = _dsUserList.Tables[0];
            }
            if (_dtUser != null && _dtUser.Rows.Count > 0)
            {
                for (int i = 0; i < _dtUser.Rows.Count; i++)
                {
                    ddlUser.Items.Add(new ListItem(_dtUser.Rows[i]["AgentCode"].ToString(), _dtUser.Rows[i]["CEID"].ToString()));
                }
                ddlUser.Items.Insert(0, new ListItem("Select", "-1"));
            }
            else
            {
                ddlUser.Items.Clear();
                ddlUser.Items.Insert(0, new ListItem("Select", "-1"));
            }
        }
        else
        {
            ddlUser.Items.Clear();
            ddlUser.Items.Insert(0, new ListItem("Select", "-1"));
        }
    }

    public void LoadBrokerTarget()
    {
        Int64 AgentId = 0;
        AgentId = Convert.ToInt64(ddlUser.Text.Trim());
        Int32 TargetYear = Convert.ToInt32(ddlTargetYear.Text);
        if (ddlUserRole.Text == "200")
        {
            AgentId = 0;
        }
        MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
        DataSet _dsAgetTarget = new DataSet();
        DataTable _dtAgetTarget = new DataTable();
        SortedList _sPara = new SortedList();
        _sPara.Add("@Step", 2);
        _sPara.Add("@AgentId", AgentId);
        _sPara.Add("@TargetYear", TargetYear);

        _dsAgetTarget = _db.GetDataSet("sprocTargetAmountAgentList", _sPara);
        if (_dsAgetTarget != null && _dsAgetTarget.Tables.Count > 0)
        {
            _dtAgetTarget = _dsAgetTarget.Tables[0];
        }
        if (_dtAgetTarget != null && _dtAgetTarget.Rows.Count > 0)
        {
            lblName.Text = _dtAgetTarget.Rows[0]["Name"].ToString();
            txtJanuary.Text = _dtAgetTarget.Rows[0]["Jan"].ToString();
            txtFebruary.Text = _dtAgetTarget.Rows[0]["Feb"].ToString();
            txtMarch.Text = _dtAgetTarget.Rows[0]["Mar"].ToString();
            txtApril.Text = _dtAgetTarget.Rows[0]["Apr"].ToString();
            txtMay.Text = _dtAgetTarget.Rows[0]["May"].ToString();
            txtJune.Text = _dtAgetTarget.Rows[0]["Jun"].ToString();
            txtJuly.Text = _dtAgetTarget.Rows[0]["Jul"].ToString();
            txtAugust.Text = _dtAgetTarget.Rows[0]["Aug"].ToString();
            txtSeptember.Text = _dtAgetTarget.Rows[0]["Sep"].ToString();
            txtOctober.Text = _dtAgetTarget.Rows[0]["Oct"].ToString();
            txtNovember.Text = _dtAgetTarget.Rows[0]["Nov"].ToString();
            txtDecember.Text = _dtAgetTarget.Rows[0]["Dec"].ToString();
            ddlStatus.Text = _dtAgetTarget.Rows[0]["StatusId"].ToString();
        }
        else
        {
            ClearFields();
        }
    }

    public void LoadBrokerListTarget()
    {
        Int64 AgentId = 0;
        Int32 TargetYear = Convert.ToInt32(ddlTargetYear.Text);
        if (txtAgentId.Text.Trim() != "-1" && txtAgentId.Text.Trim() == "0")
        {
            ddlUserRole.Text = "200";
            ddlRenewal.Text = "0";
            ddlUser.Items.Clear();
            ddlUser.Items.Insert(0, new ListItem("Admin", "0"));
        }
        else if (txtAgentId.Text.Trim() != "-1" && txtAgentId.Text.Trim() != "0")
        {
            AgentId = Convert.ToInt64(txtAgentId.Text.Trim());
        }
        if (ddlUserRole.Text == "200")
        {
            AgentId = 0;
        }
        MyDatabase _db = new MyDatabase("GGR_DBConnectionString");
        DataSet _dsAgetTarget = new DataSet();
        DataTable _dtAgetTarget = new DataTable();
        SortedList _sPara = new SortedList();
        _sPara.Add("@Step", 2);
        _sPara.Add("@AgentId", AgentId);
        _sPara.Add("@TargetYear", TargetYear);

        _dsAgetTarget = _db.GetDataSet("sprocTargetAmountAgentList", _sPara);
        if (_dsAgetTarget != null && _dsAgetTarget.Tables.Count > 0)
        {
            _dtAgetTarget = _dsAgetTarget.Tables[0];
        }
        if (_dtAgetTarget != null && _dtAgetTarget.Rows.Count > 0)
        {
            if (_dtAgetTarget.Rows[0]["AgentId"] != null && _dtAgetTarget.Rows[0]["AgentId"].ToString() != string.Empty)
            {
                ddlUserRole.Text = "100";
                string strAgentId = _dtAgetTarget.Rows[0]["AgentId"].ToString();
                string strAgentCode = _dtAgetTarget.Rows[0]["AgentCode"].ToString();
                string strRenewal = "-1";
                ddlUser.Items.Clear();
                ddlUser.Items.Insert(0, new ListItem(strAgentCode, strAgentId));
                if (strAgentId == "0")
                {
                    ddlUserRole.Text = "200";
                }
                if (_dtAgetTarget.Rows[0]["ISRenewal"] != null && _dtAgetTarget.Rows[0]["ISRenewal"].ToString() != string.Empty)
                {
                    if (strAgentId != "0")
                    {
                        ddlRenewal.Text = _dtAgetTarget.Rows[0]["ISRenewal"].ToString();
                        strRenewal = "0";
                    }
                }
                if (strRenewal != "-1")
                {
                    trIsRenewal.Visible = true;
                }
            }

            lblName.Text = _dtAgetTarget.Rows[0]["Name"].ToString();
            txtJanuary.Text = _dtAgetTarget.Rows[0]["Jan"].ToString();
            txtFebruary.Text = _dtAgetTarget.Rows[0]["Feb"].ToString();
            txtMarch.Text = _dtAgetTarget.Rows[0]["Mar"].ToString();
            txtApril.Text = _dtAgetTarget.Rows[0]["Apr"].ToString();
            txtMay.Text = _dtAgetTarget.Rows[0]["May"].ToString();
            txtJune.Text = _dtAgetTarget.Rows[0]["Jun"].ToString();
            txtJuly.Text = _dtAgetTarget.Rows[0]["Jul"].ToString();
            txtAugust.Text = _dtAgetTarget.Rows[0]["Aug"].ToString();
            txtSeptember.Text = _dtAgetTarget.Rows[0]["Sep"].ToString();
            txtOctober.Text = _dtAgetTarget.Rows[0]["Oct"].ToString();
            txtNovember.Text = _dtAgetTarget.Rows[0]["Nov"].ToString();
            txtDecember.Text = _dtAgetTarget.Rows[0]["Dec"].ToString();
            ddlStatus.Text = _dtAgetTarget.Rows[0]["StatusId"].ToString();
        }
        else
        {
            ClearFields();
        }
    }

    public string GetMonth(string month)
    {
        string result = "";
        if (month == "01")
        {
            result = " Jan";
        }
        else if (month == "02")
        {
            result = " Feb";
        }
        else if (month == "03")
        {
            result = " Mar";
        }
        else if (month == "04")
        {
            result = " Apr";
        }
        else if (month == "05")
        {
            result = " May";
        }
        else if (month == "06")
        {
            result = " Jun";
        }
        else if (month == "07")
        {
            result = " Jul";
        }
        else if (month == "08")
        {
            result = " Aug";
        }
        else if (month == "09")
        {
            result = " Sep";
        }
        else if (month == "10")
        {
            result = " Oct";
        }
        else if (month == "11")
        {
            result = " Nov";
        }
        else if (month == "12")
        {
            result = " Dec";
        }
        return result;
    }

    private void CheckPageAccess()
    {
        Int64 PageAccess = General.GetPageAccess(Convert.ToInt64(Session[General.UserId].ToString()), PageCode);
        if (PageAccess == 0)
        {
            Response.Redirect("~/UserMessage.aspx?menu=0&mt=UM&ec=101");
        }
        else if (PageAccess > 0)
        {
            EnablePageAccess(PageAccess);
        }
    }

    private void EnablePageAccess(Int64 PageAccess)
    {
        if (PageAccess == 100)
        {
            btnSubmit.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            btnSubmit.Enabled = true;
        }
    }

}
